package middlewares

import (
	"fmt"
	"net/http"
	"slices"

	"github.com/gin-gonic/gin"
	"jjqzplucky.site/sz-core/pkg/utils/token"
)

func JwtAuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		excludePath := []string{"/auth/login", "/auth/register"}
		needAuth := !slices.Contains(excludePath, c.Request.URL.Path)
		fmt.Printf("needAuth: %v, paht: %v\n", needAuth, c.Request.URL.Path)
		if needAuth {
			err := token.TokenValid(c)
			if err != nil {
				c.Status(http.StatusUnauthorized)
				c.Error(err)
				c.Abort()
				return
			}
		}
		c.Next()
	}
}
